package csc.fresher.view;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import csc.fresher.commons.BankingConstant;
import csc.fresher.entity.Customer;
import csc.fresher.entity.DepositTransaction;
import csc.fresher.entity.SystemUser;
import csc.fresher.service.AccountService;
import csc.fresher.service.DepositAccountTransactionService;
import csc.fresher.service.SystemUserService;

@Component
@Scope(value = "session")
public class SearchTransactionsManageBean {
	@Autowired
	DepositAccountTransactionService depositAccountTransactionService;
	@Autowired
	AccountService accountService;
	@Autowired
	private SystemLoginManageBean systemLoginManageBean;
	@Autowired
	private SystemUserService systemUserService;
	private SystemUser systemUser;
	private long iDCardNumber;
	private Date fDate;
	private Date tDate;
	private Customer customer = null;
	private List<DepositTransaction> listTransactions = null;

	public Customer getCustomer() {
		return customer;
	}

	public void setSystemLoginManageBean(
			SystemLoginManageBean systemLoginManageBean) {
		this.systemLoginManageBean = systemLoginManageBean;
	}

	public void setSystemUserService(SystemUserService systemUserService) {
		this.systemUserService = systemUserService;
	}

	public void onLoad() {

		if (systemLoginManageBean.getSystemUser() == null) {
			try {
				FacesContext.getCurrentInstance().getExternalContext()
						.redirect("/csc_final/templates3/Logon.jsf");
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			systemUser = systemLoginManageBean.getSystemUser();
			List<Long> listrole = systemUserService
					.getRoleByUserName(systemUser.getUserName());
			boolean result = checkRole(listrole);
			if (result == false) {
				try {
					FacesContext
							.getCurrentInstance()
							.getExternalContext()
							.redirect(
									"/csc_final/Group1/AccountManagement.xhtml");
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public boolean checkRole(List<Long> listRole) {

		for (int i = 0; i < listRole.size(); i++) {
			if ((BankingConstant.ROLE_ACCOUNT_ADMIN == listRole.get(i))
					|| (BankingConstant.ROLE_ACCOUNT_SUPPORT == listRole.get(i))) {
				return true;
			}
		}
		return false;
	}

	public List<DepositTransaction> getListTransactions() {
		return listTransactions;
	}

	public Date getfDate() {
		return fDate;
	}

	public void setfDate(Date fDate) {
		this.fDate = fDate;
	}

	public Date gettDate() {
		return tDate;
	}

	public void settDate(Date tDate) {
		this.tDate = tDate;
	}

	public long getiDCardNumber() {
		return iDCardNumber;
	}

	public void setiDCardNumber(long iDCardNumber) {
		this.iDCardNumber = iDCardNumber;
	}

	public void setDepositAccountTransactionService(
			DepositAccountTransactionService depositAccountTransactionService) {
		this.depositAccountTransactionService = depositAccountTransactionService;
	}

	public String search() {
		try {

			customer = accountService.findCustomerByIDCardNumber(iDCardNumber)
					.get(0);

		} catch (Exception ex) {
			customer = null;
		}
		try {
			Timestamp fTimeStamp = new Timestamp(fDate.getTime());
			Timestamp tTimeStamp = new Timestamp(tDate.getTime());
			listTransactions = depositAccountTransactionService
					.searchTransactionByCustomerCardIDInGivenPeriod(
							iDCardNumber, fTimeStamp, tTimeStamp);
		} catch (Exception e) {
			listTransactions = null;
		}

		return "";
	}

	public SystemUser getSystemUser() {
		return systemUser;
	}

	public void setSystemUser(SystemUser systemUser) {
		this.systemUser = systemUser;
	}

	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}
	
	

}
